<?php
/**
 * Model class for groups
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link	http://developer.resmania.com/api
 */
class RM_Groups extends RM_Model_Multilingual {
    protected $_name = 'rm_groups';
    protected $_rowClass = 'RM_Groups_Row';

    /**
     * Checks if unit is main unit of a group
     *
     * @param RM_Unit_Row $unit
     * @return bool
     */
    function isMain(RM_Unit_Row $unit){
        $group = $this->getByUnit($unit);
        if ($group == null) {
            return false;
        }
        return ($group->main_unit_id == $unit->getId());
    }

    /**
     * @param RM_Group_Row $group
     * @return Zend_Db_Table_Rowset_Abstract
     */
    function getGroupUnits(RM_Groups_Row $group){
        $unitModel = new RM_Units();
        return $unitModel->fetchAll('group_id='.$group->id);
    }

    /**
     * Returns all group units by main unit group
     *
     * @param RM_Unit_Row $unit
     * @return void
     */
    function getGroupUnitsByMain(RM_Unit_Row $unit){
        $group = $this->getByUnit($unit);
        return $this->getGroupUnits($group);
    }

    /**
     * Search group by unit
     *
     * @param RM_Unit_Row $unit
     * @return RM_Group_Row
     */
    function getByUnit(RM_Unit_Row $unit){
        if ($unit->getGroupId() == null) {
            return null;
        }
        return $this->find($unit->getGroupId())->current();
    }
}